import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/checkPasswordStrength")
public class PasswordStrengthServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置请求和响应的编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");

        // 获取密码参数
        String password = request.getParameter("password");
        String message;
        int length = password.length();

        // 根据密码长度判断安全性
        if (length < 6) {
            message = "密码长度过低，安全性低！";
        } else if (length <= 12) {
            message = "密码安全性中等！";
        } else {
            message = "密码安全性很高！";
        }

        // 构造响应数据
        Map<String, String> result = new HashMap<>();
        result.put("message", message);

        // 将结果转换为 JSON 并发送
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(result);
        response.getWriter().write(json);
    }
}
